using Aurora.AI.SqlSugar.Framework;
using Aurora.AI.SqlSugar.Framework.Repositories;
using SqlSugar;

namespace Aurora.AI.Domain.Domain.RateLimitModel.Repositories;

public sealed class RateLimitModelRepository
    : Repository<Aggregates.RateLimitModel>, IRateLimitModelRepository
{
    public Task<List<Aggregates.RateLimitModel>> GetListAsync(int page, int pageSize)
    {
        return GetDb().Queryable<Aggregates.RateLimitModel>().OrderByDescending(x => x.CreationTime)
            .ToOffsetPageAsync(page, pageSize);
    }


    public async Task<(int total, List<Aggregates.RateLimitModel> list)> GetPageListAsync(int page, int pageSize)
    {
        RefAsync<int> total = 0;
        var data = await GetDb().Queryable<Aggregates.RateLimitModel>().OrderByDescending(x => x.CreationTime)
            .ToOffsetPageAsync(page, pageSize, total);
        return (total, data);
    }

    public Task<int> GetCountAsync()
    {
        return GetDb().Queryable<Aggregates.RateLimitModel>().CountAsync();
    }

    public async Task Create(Aggregates.RateLimitModel entity)
    {
        entity.Id = Guid.NewGuid().ToString().Replace("-", "");
        entity.CreationTime = DateTime.Now;
        entity.ModificationTime = DateTime.Now;
        await InsertAsync(entity);
    }



    public async Task<Aggregates.RateLimitModel> GetByIdAsync(string id)
    {
        return await base.GetByIdAsync(id);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="application"></param>
    /// <returns></returns>
    public async Task UpdateLimit(Aggregates.RateLimitModel entity)
    {
        entity.ModificationTime = DateTime.Now;
        await UpdateAsync(entity);
    }

    public async Task<List<Aggregates.RateLimitModel>> GetAll()
    {
        return await GetDb().Queryable<Aggregates.RateLimitModel>().Where(x => x.Enable).ToListAsync();
    }

    public async Task Disable(string id)
    {

        await GetDb().Updateable<Aggregates.RateLimitModel>().SetColumns(x => !x.Enable).Where(x => x.Id.Equals(id)).ExecuteCommandAsync();
    }

    public Task Remove(string id)
    {
        return GetDb().Deleteable<Aggregates.RateLimitModel>(id).ExecuteCommandAsync();
    }

    public RateLimitModelRepository(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }
}